<?php

$time = microtime( 1 );//Calculate time in microseconds to calculate time taken to execut


//Set error stuff
        error_reporting(E_ALL);
        ini_set("display_errors", 1);
        ini_set("memory_limit", '64M' );


//Requires
        require_once( '/home/cyberpower678/stats.php' );
        require_once( "/home/cyberpower678/public_html/Smarty/languages.class.php" );
        require_once( "/home/cyberpower678/public_html/Smarty/Smarty.class.php" );
        require_once( '/home/cyberpower678/public_html/sitenotice.php' );
        require_once( '/home/cyberpower678/Peachy/Init.php' );
        require_once( '/home/cyberpower678/database.inc' );


//Store stats
        addStatV2( 'ArticleInfo' );


//Initialize Smarty
        $phptemp = new Smarty();
        $content = new Smarty();
        
        $language = new Language( array( "en" ) );
        $curlang = $language->getLang();
        
        $langlinks = $language->generateLangLinks();
        
        $phptemp->config_load( $curlang . '.conf', 'articleinfo' );
        $content->config_load( $curlang . '.conf', 'articleinfo' );


//Initialize and check sitenotice
        $siteNoticeClass = new siteNotice;
        $sitenotice = $siteNoticeClass->checkSiteNoticeHtml();
        if( $sitenotice ) {
                $phptemp->assign( "alert", $sitenotice );
        }


//Initialize WebRequest class from Peachy
$wgRequest = new WebRequest();


//Show form if &article parameter is not set (or empty)
        if( !$wgRequest->getSafeVal( 'getBool', 'article' ) ) {
                $content->assign( 'form', $curlang );
                assignContent();
        }

//Now load configs for the graph templates
        $linegraph = new Smarty();
        $sizegraph = new Smarty();
        $linegraph->config_load( $curlang . '.conf', 'articleinfo' );
        $sizegraph->config_load( $curlang . '.conf', 'articleinfo' );


//Set the three main variables
        $article = trim( str_replace( array('&#39;','%20'), array('\'',' '), $wgRequest->getSafeVal( 'article' ) ) );
        $article = urldecode($article);
        $wiki = $wgRequest->getSafeVal( 'wiki', 'wikipedia' );
        $lang = $wgRequest->getSafeVal( 'lang', 'en' );
        $url = $lang.'.'.$wiki.'.org';


//Load database
        $dbr = new Database( 
                'sql-toolserver', 
                $toolserver_username, 
                $toolserver_password, 
                'toolserver'
        );
        
        $res = $dbr->select(
                'wiki',
                array( 'dbname', 'server', ),
                array( 'domain' => "$lang.$wiki.org" )
        );
                
        if( !count( $res ) ) {
                toDie( $phptemp->get_config_vars( 'nowiki', $url ) );
        }
        
        $dbr = new Database( 
                'sql-s' . $res[0]['server'], 
                $toolserver_username, 
                $toolserver_password, 
                $res[0]['dbname']
        );



//Initialize Peachy
        $pgVerbose = array();
        /*$pgHooks['StartLogin'][] = 'fixlogin';
        function fixlogin( &$config ) {
                $config['httpecho'] = true;
        }*/
        $site = Peachy::newWiki( null, null, null, 'http://'.$url.'/w/api.php' );
        
        try {
                $pageClass = $site->initPage( $article, null, !$wgRequest->getSafeVal( 'getBool', 'nofollowredir' ) );
        } catch( BadTitle $e ) {
                toDie( $phptemp->get_config_vars( 'nosuchpage', $e->getTitle() ) );
        } catch( Exception $e ) {
                toDie( $e->getMessage() );
        }


//Check for page existance
        $phptemp->assign( "page", $pageClass->get_title() );
        
        if( !$pageClass->exists() ) toDie( $phptemp->get_config_vars( 'nosuchpage', $pageClass->get_title() ) );


//Start doing the DB request
        $conds = array( 'rev_page = ' . $dbr->strencode( $pageClass->get_id() ) );
        $start = $end = false;
        
        if( $wgRequest->getSafeVal( 'getBool', 'begin' ) ) {
                $conds[] = 'UNIX_TIMESTAMP(rev_timestamp) > ' . $dbr->strencode( strtotime( $wgRequest->getSafeVal( 'begin' ) ) );
                $start = ( $wgRequest->getSafeVal( 'getBool', 'begin' ) ) ? $wgRequest->getSafeVal( 'begin' ) : null;
        }
        if( $wgRequest->getSafeVal( 'getBool', 'end' ) ) {
                $conds[] = 'UNIX_TIMESTAMP(rev_timestamp) < ' . $dbr->strencode( strtotime( $wgRequest->getSafeVal( 'endn' ) ) );
                $end = ( $wgRequest->getSafeVal( 'getBool', 'end' ) ) ? $wgRequest->getSafeVal( 'end' ) : null;
        }
        
        $history = $dbr->select( 
                array( 'revision' ),
                array( 'rev_user_text', 'rev_user', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_len' ),
                $conds,
                array( 'LIMIT' => 50000 )
        );
        
        if( !count( $history ) ) toDie( $phptemp->get_config_vars( 'norevisions' ) );
        if( count( $history ) == 50000 ) $content->assign( "notice", $phptemp->get_config_vars( 'toomanyrevisions' ) );


//Get logs, for Edits over Time graph
        $logsi = $site->logs( null, false, $pageClass->get_title(), strtotime( $start ), strtotime( $end ), 'older', false, array( 'type', 'timestamp', 'user', 'details' ) ); 
        $logs = array();
        
        foreach( $logsi as $log ) {
                if( in_array( $log['type'], array( 'delete', 'move', 'protect' ) ) && !in_array( $log['action'], array( 'revision' ) ) ) {
                        if( !isset( $logs[date('nY', strtotime( $log['timestamp'] ))][$log['action']] ) ) {
                                $logs[date('nY', strtotime( $log['timestamp'] ))][$log['action']] = 0;
                        }
                        
                        $logs[date('nY', strtotime( $log['timestamp'] ))][$log['action']]++;
                }
        }
        
        unset( $logsi );
        
        foreach( $logs as $date => $log ) {
                arsort( $log );
                $logs[$date] = actionParse( $date, $log );
        }


//Now we can start our master array. This one will be HUGE!
        $data = array(
                'first_edit' => array(
                        'timestamp' => $history[0]['rev_timestamp'],
                        'user' => $history[0]['rev_user_text']
                ),
                'year_count' => array(),
                'count' => 0,
                'editors' => array(),
                'anons' => array(),
                'year_count' => array(),
                'minor_count' => 0,
                'count_history' => array( 'today' => 0, 'week' => 0, 'month' => 0, 'year' => 0 )
        );
        
        $first_edit_parse = date_parse( $data['first_edit']['timestamp'] );




//And now comes the logic for filling said master array
        foreach( $history as $id => $rev ) {
                $data['last_edit'] = $rev['rev_timestamp'];
                $data['count']++;
                
                //Sometimes, with old revisions (2001 era), the revisions from 2002 come before 2001
                if( strtotime( $rev['rev_timestamp'] ) < strtotime( $data['first_edit']['timestamp'] ) ) {      
                        $data['first_edit'] = array(
                                'timestamp' => $rev['rev_timestamp'],
                                'user' => htmlspecialchars( $rev['rev_user_text'] )
                        );
                        
                        $first_edit_parse = date_parse( $data['first_edit']['timestamp'] );
                }
                
                
                $timestamp = date_parse( $rev['rev_timestamp'] );
                
                
                //Fill in the blank arrays for the year and 12 months
                if( !isset( $data['year_count'][$timestamp['year']] ) ) {
                        $data['year_count'][$timestamp['year']] = array( 'all' => 0, 'minor' => 0, 'anon' => 0, 'months' => array() );
                        
                        for( $i = 1; $i <= 12; $i++ ) {
                                $data['year_count'][$timestamp['year']]['months'][$i] = array( 'all' => 0, 'minor' => 0, 'anon' => 0, 'size' => array() );
                        }
                }
                
                //Increment counts
                $data['year_count'][$timestamp['year']]['all']++;
                $data['year_count'][$timestamp['year']]['months'][$timestamp['month']]['all']++;
                $data['year_count'][$timestamp['year']]['months'][$timestamp['month']]['size'][] = number_format( ( $rev['rev_len'] / 1024 ), 2 );
                
                
                //Now to fill in various user stats
                $username = htmlspecialchars($rev['rev_user_text']);
                if( !isset( $data['editors'][$username] ) ) {
                        $data['editors'][$username] = array(    
                                'all' => 0, 
                                'minor' => 0, 
                                'first' => date( 'd F Y, H:i:s', strtotime( $rev['rev_timestamp'] ) ), 
                                'last' => null, 
                                'atbe' => null, 
                                'minorpct' => 0, 
                                'size' => array(), 
                                'urlencoded' => str_replace( array( '+' ), array( '_' ), urlencode( $rev['rev_user_text'] ) )
                        );
                }
                
                //Increment these counts...
                $data['editors'][$username]['all']++;   
                $data['editors'][$username]['last'] = date( 'd F Y, H:i:s', strtotime( $rev['rev_timestamp'] ) );       
                $data['editors'][$username]['size'][] = number_format( ( $rev['rev_len'] / 1024 ), 2 );
                
                if( !$rev['rev_user'] ) {
                        //Anonymous, increase counts
                        $data['anons'][] = $username;
                        $data['year_count'][$timestamp['year']]['anon']++;
                        $data['year_count'][$timestamp['year']]['months'][$timestamp['month']]['anon']++;
                }
                
                if( $rev['rev_minor_edit'] ) {
                        //Logged in, increase counts
                        $data['minor_count']++;
                        $data['year_count'][$timestamp['year']]['minor']++;
                        $data['year_count'][$timestamp['year']]['months'][$timestamp['month']]['minor']++;
                        $data['editors'][$username]['minor']++; 
                }
                
                
                //Increment "edits per <time>" counts
                if( strtotime( $rev['rev_timestamp'] ) > strtotime( '-1 day' ) ) $data['count_history']['today']++;
                if( strtotime( $rev['rev_timestamp'] ) > strtotime( '-1 week' ) ) $data['count_history']['week']++;
                if( strtotime( $rev['rev_timestamp'] ) > strtotime( '-1 month' ) ) $data['count_history']['month']++;
                if( strtotime( $rev['rev_timestamp'] ) > strtotime( '-1 year' ) ) $data['count_history']['year']++;
                
        }


//Fill in years with no edits
        for( $year = $first_edit_parse['year']; $year <= date( 'Y' ); $year++ ) {
                if( !isset( $data['year_count'][$year] ) ) {
                        $data['year_count'][$year] = array( 'all' => 0, 'minor' => 0, 'anon' => 0, 'months' => array() );
                        
                        for( $i = 1; $i <= 12; $i++ ) {
                                $data['year_count'][$year]['months'][$i] = array( 'all' => 0, 'minor' => 0, 'anon' => 0, 'size' => array() );
                        }
                }
        }


//Add more general statistics
        $data['totaldays'] = floor( ( strtotime( $data['last_edit'] ) - strtotime( $data['first_edit']['timestamp'] ) ) / 60 / 60 / 24 );
        $data['average_days_per_edit'] = number_format( $data['totaldays'] / $data['count'], 2 );
        $data['edits_per_month'] = ( $data['totaldays'] ) ? number_format( $data['count'] / ( $data['totaldays'] / ( 365/12 ) ), 2 ) : 0;
        $data['edits_per_year'] =( $data['totaldays'] ) ? number_format( $data['count'] / ( $data['totaldays'] / 365 ) , 2 ) : 0;
        $data['edits_per_editor'] = number_format( $data['count'] / count( $data['editors'] ) , 2 );
        $data['editor_count'] = count( $data['editors'] );
        $data['anon_count'] = count( $data['anons'] );


//Various sorts
        arsort( $data['editors'] );
        ksort( $data['year_count'] );



//Fix the year counts
        $num = 0;
        $cum = 0;
        $scum = 0;
        
        foreach( $data['year_count'] as $year => $months ) {
                
                //Unset months before the first edit and after the last edit
                foreach( $months['months'] as $month => $tmp ) {
                        if( $year == $first_edit_parse['year'] ) {
                                if( $month < $first_edit_parse['month'] ) unset( $data['year_count'][$year]['months'][$month] );
                        }
                        if( $year == date( 'Y' ) ) {
                                if( $month > date( 'm' ) ) unset( $data['year_count'][$year]['months'][$month] );
                        }
                }
                
                
                //Calculate anon/minor percentages
                $data['year_count'][$year]['pcts']['anon'] = ( $data['year_count'][$year]['all'] ) ? number_format( ( $data['year_count'][$year]['anon'] / $data['year_count'][$year]['all'] ) * 100, 2 ) : 0.00;
                $data['year_count'][$year]['pcts']['minor'] = ( $data['year_count'][$year]['all'] ) ? number_format( ( $data['year_count'][$year]['minor'] / $data['year_count'][$year]['all'] ) * 100, 2 ) : 0.00;
                
                
                //Continue with more stats...
                foreach( $data['year_count'][$year]['months'] as $month => $tmp ) {
                
                        //More percentages...
                        $data['year_count'][$year]['months'][$month]['pcts']['anon'] = ( $tmp['all'] ) ? number_format( ( $tmp['anon'] / $tmp['all'] ) * 100, 2 ) : 0.00;
                        $data['year_count'][$year]['months'][$month]['pcts']['minor'] = ( $tmp['all'] ) ? number_format( ( $tmp['minor'] / $tmp['all'] ) * 100, 2 ): 0.00;
                        
                        //XID and cumulative are used in the flash graph
                        $data['year_count'][$year]['months'][$month]['xid'] = $num;
                        $data['year_count'][$year]['months'][$month]['cumulative'] = $cum + $tmp['all'];
                        
                        if( count( $tmp['size'] ) ) {
                                $data['year_count'][$year]['months'][$month]['size'] = number_format( ( array_sum( $tmp['size'] ) / count( $tmp['size'] ) ), 2 );
                        }
                        else {
                                $data['year_count'][$year]['months'][$month]['size'] = 0;
                        }
                        
                        $data['year_count'][$year]['months'][$month]['sizecumulative'] = $scum + $data['year_count'][$year]['months'][$month]['size'];
                        $num++;
                        $cum += $tmp['all'];
                        $scum += $data['year_count'][$year]['months'][$month]['size'];
                }
        }


//Top 10% info
        $data['top_ten'] = array( 'editors' => array(), 'count' => 0 );
        $data['top_fifty'] = array();


//Now to fix the user info...
        $tmp = $tmp2 = 0;
        foreach( $data['editors'] as $editor => $info ) {
                
                //Is the user in the top 10%?
                if( $tmp <= (int)( count( $data['editors'] ) * 0.1 ) ) {
                        $data['top_ten']['editors'][] = $editor;
                        $data['top_ten']['count'] += $info['all'];
                        
                        $tmp++;
                }
                
                //Is the user in the 50 highest editors?
                if( $tmp < 50 ) {
                        $data['top_fifty'][] = $editor;
                }
                
                $data['editors'][$editor]['minorpct'] = ( $info['all'] ) ? number_format( ( $info['minor'] / $info['all'] ) * 100, 2 ): 0.00;
                
                if( $info['all'] > 1 ) {
                        $data['editors'][$editor]['atbe'] = getTimeString( (int)( ( strtotime( $info['last'] ) - strtotime( $info['first'] ) ) / $info['all'] ));
                }
                
                if( count( $info['size'] ) ) {
                        $data['editors'][$editor]['size'] = number_format( ( array_sum( $info['size'] ) / count( $info['size'] ) ), 2 );
                }
                else {
                        $data['editors'][$editor]['size'] = 0;
                }
                
                $tmp2++;
        }


//Now we can assign the Smarty variables!
        $content->assign( "info", true );
                $content->assign( "page", $pageClass->get_title() );
                $content->assign( "urlencodedpage", str_replace( '+', '_', urlencode( $pageClass->get_title() ) ) );
                $content->assign( "totaledits", number_format( $data['count'] ) );
                $content->assign( "minoredits", number_format( $data['minor_count'] ) );
                $content->assign( "minoredits", number_format( $data['minor_count'] ) );
                $content->assign( "anonedits", number_format( $data['anon_count'] ) );
                $content->assign( "minorpct", number_format( ( $data['minor_count'] / $data['count'] ) * 100, 2 ) );
                $content->assign( "anonpct", number_format( ( $data['anon_count'] / $data['count'] ) * 100, 2 ) );
                $content->assign( "firstedit", date( 'd F Y, H:i:s', strtotime( $data['first_edit']['timestamp'] ) ) );
                $content->assign( "firstuser", $data['first_edit']['user'] );
                $content->assign( "lastedit", date( 'd F Y, H:i:s', strtotime( $data['last_edit'] ) ) );
                $content->assign( "timebwedits", $data['average_days_per_edit'] );
                $content->assign( "editspermonth", $data['edits_per_month'] );
                $content->assign( "editsperyear", $data['edits_per_year'] );
                $content->assign( "lastday", number_format( $data['count_history']['today'] ) );
                $content->assign( "lastweek", number_format( $data['count_history']['week'] ) );
                $content->assign( "lastmonth", number_format( $data['count_history']['month'] ) );
                $content->assign( "lastyear", number_format( $data['count_history']['year'] ) );
                $content->assign( "editorcount", number_format( $data['editor_count'] ) );
                $content->assign( "editsperuser", number_format( ( $data['editor_count'] / $data['count'] ), 2 ) );
                $content->assign( "toptencount", number_format( $data['top_ten']['count'] ) );
                $content->assign( "toptenpct", number_format( ( $data['top_ten']['count'] / $data['count'] ) * 100, 2 ) );
        
        
        $content->assign( "graphs", true );
                $content->assign( "graphanonpct", number_format( ( $data['anon_count'] / $data['count'] ) * 100, 2 ) );
                $content->assign( "graphuserpct", number_format( 100 - ( ( $data['anon_count'] / $data['count'] ) * 100 ), 2 ) );
                $content->assign( "graphminorpct", number_format( ( $data['minor_count'] / $data['count'] ) * 100, 2 ) );
                $content->assign( "graphmajorpct", number_format( 100 - ( ( $data['minor_count'] / $data['count'] ) * 100 ), 2 ) );
                $content->assign( "graphtoptenpct", number_format( ( $data['top_ten']['count'] / $data['count'] ) * 100, 2 ) );
                $content->assign( "graphbottomninetypct", number_format( 100 - ( ( $data['top_ten']['count'] / $data['count'] ) * 100 ), 2 ) );
        
        $content->assign( "yeargraph", true );
                $content->assign( "yearcounts", $data['year_count'] );
                $content->assign( "yearpixels", getYearPixels( $data['year_count'] ) );
                $content->assign( "pixelcolors", array( 'all' => '008800', 'anon' => '55FF55', 'minor' => 'FFFF55' ) );
        
        $content->assign( "linegraph", true );
                $linegraph->assign( "data", $data['year_count'] );
                $linegraph->assign( "eventdata", $logs );
                $content->assign( "linegraphdata", md5( $pageClass->get_title() . '-' . $pageClass->get_id() ) );
                file_put_contents( 'data/' . md5( $pageClass->get_title() . '-' . $pageClass->get_id() ) . '.xml', $linegraph->fetch( 'linegraph.tpl' ));
        
        $content->assign( "monthgraph", true );
                $content->assign( "monthpixels", getMonthPixels( $data['year_count'] ) );
                $content->assign( "evenyears", getEvenYears( array_keys( $data['year_count'] ) ) );
        
        $content->assign( "sizegraph", true );
                $sizegraph->assign( "data", $data['year_count'] );
                $content->assign( "sizegraphdata", md5( $pageClass->get_title() . '-' . $pageClass->get_id() . '-line' ) );
                file_put_contents( 'data/' . md5( $pageClass->get_title() . '-' . $pageClass->get_id()  . '-line' ) . '.xml', $sizegraph->fetch( 'sizegraph.tpl' ));
                
        $content->assign( "usertable", true );
                $content->assign( "userdata", $data['editors'] );
                $content->assign( "topteneditors", $data['top_fifty'] );
                $content->assign( "url", $url );
                $content->assign( "lang", $lang );
                $content->assign( "wiki", $wiki );

//Calculate replag
        $replag = getReplag();
        
        if ($replag[0] > 120) {
                $content->assign( 'replag', $phptemp->get_config_vars( 'highreplag', $replag[1] ) );
        }
        
//Calculate time taken to execute, and finish up the script
        $exectime = number_format(microtime( 1 ) - $time, 2, '.', '');
        $phptemp->assign( "excecutedtime", $phptemp->get_config_vars( 'executed', $exectime ) );
        assignContent();


//Script finished, now we're on to the functions

//If there is a failure, do it pretty.
        function toDie( $msg ) {
                global $content, $phptemp;
                $content->assign( "error", $msg );
                assignContent();
        }

//Debugging stuff
        function pre( $array ) {
                echo "<pre>";
                print_r( $array );
                echo "</pre>";
        }

//Parse the templates and die
        function assignContent() {
                global $phptemp, $content, $curlang, $langlinks;
                $phptemp->assign( "source2", "https://code.google.com/p/soxred93tools/source/browse/trunk/web/articleinfo/index.php" );
                $phptemp->assign( "curlang", $curlang );
                $phptemp->assign( "langlinks", $langlinks );
                $phptemp->assign( "content", $content->fetch( 'articleinfo.tpl' ) );
                $phptemp->display( '../../templates/mainSmarty.tpl' ); 
                die();
        }


//Converts XXXXXXXXX seconds to X days, X hours, X minutes, X seconds
        function getTimeString( $secs ) {
                global $phptemp;
                
                $second = 1;
                $minute = $second * 60;
                $hour = $minute * 60;
                $day = $hour * 24;
                $week = $day * 7;
                $month = $day * ( 365 / 12 );
                
                $r = array();
                if ($secs > $month) {
                        $count = 0;
                        for( $i = $month; $i <= $secs; $i += $month ) {
                                $count++;
                        }
                        
                        $r[] = $count . ' ' . $phptemp->get_config_vars( 'mo' );
                        $secs -= $month * $count;
                }
                if ($secs > $week) {
                        $count = 0;
                        for( $i = $week; $i <= $secs; $i += $week ) {
                                $count++;
                        }
                        
                        $r[] = $count . ' ' . $phptemp->get_config_vars( 'w' );
                        $secs -= $week * $count;
                }
                if ($secs > $day) {
                        $count = 0;
                        for( $i = $day; $i <= $secs; $i += $day ) {
                                $count++;
                        }
                        
                        $r[] = $count . ' ' . $phptemp->get_config_vars( 'd' );
                        $secs -= $day * $count;
                }
                if ($secs > $hour) {
                        $count = 0;
                        for( $i = $hour; $i <= $secs; $i += $hour ) {
                                $count++;
                        }
                        
                        $r[] = $count . ' ' . $phptemp->get_config_vars( 'h' );
                        $secs -= $hour * $count;
                }
                if ($secs > $minute) {
                        $count = 0;
                        for( $i = $minute; $i <= $secs; $i += $minute ) {
                                $count++;
                        }
                        
                        $r[] = $count . ' ' . $phptemp->get_config_vars( 'm' );
                        $secs -= $minute * $count;
                }
                if ($secs) {
                        $r[] = $secs . ' ' . $phptemp->get_config_vars( 's' );
                }
                
                $r = implode( ', ', $r );
                return $r;
        }

//Calculate how many pixels each year should get for the Edits per Year table
        function getYearPixels( &$data ) {
                $month_total_edits = array();
                foreach( $data as $year => $tmp ) {
                        $month_total_edits[$year] = $tmp['all'];
                }
                
                $max_width = max( $month_total_edits );
                
                $pixels = array();
                foreach( $data as $year => $tmp ) {
                        if( $tmp['all'] == 0 ) $pixels[$year] = array();
                        
                        $processarray = array( 'all' => $tmp['all'], 'anon' => $tmp['anon'], 'minor' => $tmp['minor'] );
                        
                        asort( $processarray );
                        
                        foreach( $processarray as $type => $count ) {
                                $newtmp = ceil( 500 * ( $count ) / $max_width );
                                $pixels[$year][$type] = $newtmp;
                        }
                }
                
                return $pixels;
        }

//Calculate how many pixels each month should get for the Edits per Month table
        function getMonthPixels( &$data ) {
                $month_total_edits = array();
                foreach( $data as $year => $tmp ) {
                        foreach( $tmp['months'] as $month => $newdata ) {
                                $month_total_edits[ $month.'/'.$year ] = $newdata['all'];
                        }
                }
        
                $max_width = max( $month_total_edits );
                
                $pixels = array();
                foreach( $data as $year => $tmp ) {
                        foreach( $tmp['months'] as $month => $newdata ) {
                                if( $tmp['all'] == 0 ) $pixels[$year][$month] = array();
                                
                                $processarray = array( 'all' => $newdata['all'], 'anon' => $newdata['anon'], 'minor' => $newdata['minor'] );
                                
                                asort( $processarray );
                                
                                foreach( $processarray as $type => $count ) {
                                        $newtmp = ceil( ( 500 * ( $count ) / $max_width ) );
                                        $pixels[$year][$month][$type] = $newtmp;
                                }
                        }
                }
                
                return $pixels;
        }


//Generate the log actions infobox for the flash graph
        function actionParse( $date, $logs ) {
                global $content;
                
                if( strlen( $date ) == 5 ) {
                        $parseddate = '0' . substr( $date, 0, 1 ) . '/' . substr( $date, 1 );
                }
                else {
                        $parseddate = substr( $date, 0, 2 ) . '/' . substr( $date, 2 );
                }
                
                $ret = $content->get_config_vars( 'duringdate', $parseddate );
                
                $ret .= "<ul>";
                
                foreach( $logs as $type => $count ) {
                        switch( $type ) {
                                case 'modify':
                                        $ret .= "<li>" . $content->get_config_vars( 'linegraphmodify', $count ) . "</li>";
                                        break;
                                case 'protect':
                                        $ret .= "<li>" . $content->get_config_vars( 'linegraphprotect', $count ) . "</li>";
                                        break;
                                case 'unprotect':
                                        $ret .= "<li>" . $content->get_config_vars( 'linegraphunprotect', $count ) . "</li>";
                                        break;
                                case 'move':
                                        $ret .= "<li>" . $content->get_config_vars( 'linegraphmove' . $type, $count ) . "</li>";
                                        break;
                                case 'move_redir':
                                        $ret .= "<li>" . $content->get_config_vars( 'linegraphmoveredir' . $type, $count ) . "</li>";
                                        break;
                                case 'move_prot':
                                        $ret .= "<li>" . $content->get_config_vars( 'linegraphmoveprot' . $type, $count ) . "</li>";
                                        break;
                                case 'delete':
                                        $ret .= "<li>" . $content->get_config_vars( 'linegraphdelete' . $type, $count ) . "</li>";
                                        break;
                                case 'restore':
                                        $ret .= "<li>" . $content->get_config_vars( 'linegraphundelete' . $type, $count ) . "</li>";
                                        break;
                                default:
                                        break;
                        }
                }
                
                $ret .= "</ul>";
                
                return htmlentities( $ret );
        
        }


//Returns a list of even years, used to generate contrasting colors for the Edits/Month table
        function getEvenYears( $years ) {
                $years = array_flip( $years );
                foreach( $years as $year => $id ) {
                        $years[$year] = "5";
                        if( $year % 2 == 0 ) unset( $years[$year] );
                }
                return $years;
        }


//Calculate how much replag there is
        function getReplag() {
                global $dbr;
                
                $res = $dbr->select(
                        'recentchanges',
                        'UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp) AS replag',
                        null,
                        array( 
                                'ORDER BY' => 'rc_timestamp DESC',
                                'LIMIT' => 1
                        )
                );
                
                $seconds = floor( $res[0]['replag'] );
                $text = getTimeString( $seconds );
                
                return array( $seconds, $text );
                
        }

